from build_in import os, datetime, logging


basedir = os.path.abspath(os.path.dirname(__file__))


def log_file_handler(project_name, level):
    # 根目录
    _root_path = basedir[:basedir.find(project_name) + len(project_name)]
    # 日志文件名称
    _file_name = datetime.strftime(datetime.now(), '%Y%m%d')
    # 拼接日志路径
    _log_path = os.path.join(_root_path, 'logs\\' + _file_name + '.txt')
    # 日志输出处理对象
    handler = logging.FileHandler(_log_path, encoding='UTF-8')
    # 日志格式
    logging_format = logging.Formatter('%(asctime)s-%(levelname)s-%(filename)s-%(funcName)s-%(lineno)s-%(message)s')
    handler.setFormatter(logging_format)
    # 对象添加log的级别
    handler.setLevel(level)
    return handler


if __name__ == '__main__':
    root_path = basedir[:basedir.find("my-python-app") + len("my-python-app")]
    filename = datetime.strftime(datetime.now(), '%Y%m')
    print(os.path.join(root_path, 'logs_' + filename + '.txt'))
